package org.kiwi.job.orm.service.impl;

import org.apache.commons.collections4.map.HashedMap;
import org.kiwi.job.orm.entity.JobLog;
import org.kiwi.job.orm.model.JobLogModel;
import org.kiwi.job.orm.repository.JobLogRepository;
import org.kiwi.job.orm.service.JobLogService;
import org.kiwi.util.mapping.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @email jack.liu.19910921@gmail.com
 * Created by jack on 17/2/14.
 */
@Service
public class JobLogServiceImpl implements JobLogService {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private JobLogRepository jobLogRepository;


    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int deleteByPrimaryKey(Long id) {
        return jobLogRepository.deleteByPrimaryKey(id);
    }


    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
	 * but you still have to take care of @Transactional for multi-statements scenario.
	 * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
	 */
    @Override
    public JobLogModel findByPrimaryKey(Long id) {
        JobLog JobLog = jobLogRepository.selectByPrimaryKey(id);
        return beanMapper.map(JobLog, JobLogModel.class);
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int updateByPrimaryKey(JobLogModel JobLogModel) {
        return jobLogRepository.updateByPrimaryKey(beanMapper.map(JobLogModel, JobLog.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int updateByPrimaryKeySelective(JobLogModel JobLogModel) {
        return jobLogRepository.updateByPrimaryKeySelective(beanMapper.map(JobLogModel, JobLog.class));
    }


    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int create(JobLogModel JobLogModel) {
        return jobLogRepository.insert(beanMapper.map(JobLogModel, JobLog.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int createSelective(JobLogModel JobLogModel) {
        return jobLogRepository.insertSelective(beanMapper.map(JobLogModel, JobLog.class));
    }

    /*
     * @Transactional is not necessary for the single atomic CRUD statement for better performance,
     * but you still have to take care of @Transactional for multi-statements scenario.
     * if read only,please config as "@Transactional(readOnly = true)",otherwise "@Transactional"
     */
    @Override
    public int selectCount(JobLogModel JobLogModel) {
        return jobLogRepository.selectCount(beanMapper.map(JobLogModel, JobLog.class));
    }

    @Override
    public List<JobLogModel> selectPageByJobCode(String jobCode, Long pageNum, Long pageSize) {
        Map params = new HashedMap();

        JobLog jobLog = new JobLog();
        jobLog.setJobCode(jobCode);
        jobLog.setIsDeleted((byte) 0);

        params.put("jobLog", jobLog);
        params.put("start", (pageSize.intValue() - 1) * pageNum.intValue());
        params.put("limit", pageSize.intValue());

        return beanMapper.mapAsList(jobLogRepository.selectPage(params), JobLogModel.class);
    }

    @Override
    public List<JobLogModel> selectByJobCodeLastHour(String jobCode) {
        return beanMapper.mapAsList(jobLogRepository.selectByJobCodeLastHour(jobCode), JobLogModel.class);
    }

}
